package io.renren.sf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.sf.dao.HzFileDao;
import io.renren.sf.entity.CampusEntity;
import io.renren.sf.entity.HzFileEntity;
import io.renren.sf.service.HzFileService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("hzFileService")
public class HzFileServiceImpl extends ServiceImpl<HzFileDao, HzFileEntity> implements HzFileService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<HzFileEntity> page = this.page(
                new Query<HzFileEntity>().getPage(params),
                new QueryWrapper<HzFileEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public boolean createdir(long upperFid, String fileName, long userId, int fileType) {
        HzFileEntity upperDir;
        if (upperFid == 0 || ((upperDir = this.baseMapper.selectById(upperFid)) != null && upperDir.getFileType() == 0)) {
            List<HzFileEntity> list = this.baseMapper.selectList(new QueryWrapper<HzFileEntity>().eq("tid", userId).eq("file_type", fileType).eq("file_name", fileName));
            if (list == null) {
                HzFileEntity hzFileEntity = new HzFileEntity(upperFid, fileName, userId, fileType);
                this.baseMapper.insert(hzFileEntity);
                return true;
            }
        }
        return false;
    }

    @Transactional
    @Override
    public boolean createfile(String fileAddress, long upperFid, String fileName, long userId, int fileType, String fileSize) {
        HzFileEntity upperDir = this.baseMapper.selectById(upperFid);
        if (upperDir != null && upperDir.getFileType() == 0) {
            List<HzFileEntity> list = this.baseMapper.selectList(new QueryWrapper<HzFileEntity>().eq("tid", userId).eq("file_type", fileType).eq("file_name", fileName));
            if (list == null) {
                HzFileEntity hzFileEntity = new HzFileEntity(fileAddress, upperFid, fileName, userId, fileType, fileSize);
                this.baseMapper.insert(hzFileEntity);
                return true;
            }
        }
        return false;
    }

    @Override
    public List<HzFileEntity> queryfilesbyfid(long upperFid, long userId) {
        return this.baseMapper.selectList(new QueryWrapper<HzFileEntity>().eq("upper_fid", upperFid).eq("file_belongto", 1).orderByAsc("file_type"));
    }

    @Override
    public List<HzFileEntity> queryfilesbyname(String filename, long userId, int fileBelongto) {
        return this.baseMapper.selectList(new QueryWrapper<HzFileEntity>().eq("tid", userId).eq("file_belongto", fileBelongto).like("file_name", "%" + filename + "%").orderByAsc("file_type"));
    }

    @Override
    public List<HzFileEntity> queryfilesbystate(int verifyState, long userId) {
        if (verifyState == 3) {
            return this.baseMapper.queryallverifyfiles(userId);
        } else if (verifyState == 1 || verifyState == 2 || verifyState == 0)
            return this.baseMapper.selectList(new QueryWrapper<HzFileEntity>().eq("tid", userId).eq("verify_state", verifyState));
        return null;
    }

    @Override
    public boolean deletebyfid(long fid, long userId) {
        String ids = this.baseMapper.getDeleteFilesIds(fid, userId);
        this.baseMapper.deletebyfids(ids);
        return true;
    }


    @Transactional
    @Override
    public boolean deletefilesbyids(List<Long> ids, long userId) {
        for (long id : ids) {
            HzFileEntity hzFileEntity = this.baseMapper.selectById(id);
            if (hzFileEntity != null && hzFileEntity.getFileType() == 0) {
                String idstr = this.baseMapper.getDeleteFilesIds(id, userId);
                this.baseMapper.deletebyfids(idstr);
            } else if (hzFileEntity != null && hzFileEntity.getFileType() == 0)
                this.baseMapper.deleteById(id);
        }
        return true;
    }

    @Override
    public boolean addfiledowncount(long fid) {
        this.baseMapper.addfiledowncount(fid);
        return true;
    }

//公共资源

    @Override
    public List<HzFileEntity> querypublicfilesbyfid(long upperFid, long userId) {
        return this.baseMapper.selectList(new QueryWrapper<HzFileEntity>().eq("upper_fid", upperFid).eq("file_belongto", 0).orderByAsc("file_type"));
    }

    @Override
    public List<HzFileEntity> querypublicfilesbydowncount() {
        return this.baseMapper.selectList(new QueryWrapper<HzFileEntity>().eq("file_belongto", 0).eq("file_type", 1).notIn("verify_state", 0, 2).orderByDesc("file_loaddown_count"));
    }

    @Override
    public List<HzFileEntity> querypublicfilesbyfidpca(long upperFid, long userId, Integer pcatype, Long pcaid) {
        //0：省1：市2：区/县
        if (pcatype == 0)
            return this.baseMapper.selectList(new QueryWrapper<HzFileEntity>().eq("pid", pcaid).eq("file_type", 1).eq("file_belongto", 0).orderByDesc("file_loaddown_count"));
        else if (pcatype == 1)
            return this.baseMapper.selectList(new QueryWrapper<HzFileEntity>().eq("cid", pcaid).eq("file_type", 1).eq("file_belongto", 0).orderByDesc("file_loaddown_count"));
        else if (pcatype == 2)
            return this.baseMapper.selectList(new QueryWrapper<HzFileEntity>().eq("aid", pcaid).eq("file_type", 1).eq("file_belongto", 0).orderByDesc("file_loaddown_count"));
        return null;
    }

    @Override
    public boolean changefiletodir(long fid, long did, long userId) {
        HzFileEntity hzFileEntity = new HzFileEntity();
        hzFileEntity.setId(fid);
        hzFileEntity.setUpperFid(did);
        hzFileEntity.setUpdateTime(new Date());
        this.baseMapper.update(hzFileEntity, new QueryWrapper<HzFileEntity>().eq("tid", userId));
        return false;
    }

    @Override
    public boolean deleteFile(List<Long> ids) {
        return baseMapper.deleteFile(ids);
    }

    @Override
    public Page<HzFileEntity> getPublicFile(Map<String, Object> params) {
        String upperFid = (String) params.get("upperFid");
        String fileName = (String) params.get("fileName");
        String pcatype = (String) params.get("pcatype");
        String pcaid = (String) params.get("pcaid");
        long page = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        Page<HzFileEntity> filePage = new Page<>(page, limit);
        QueryWrapper<HzFileEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(upperFid)) {
            queryWrapper.eq("upper_fid", Long.parseLong(upperFid));
        }
        if(!StringUtils.isEmpty(fileName)) {
            queryWrapper.like("file_name", fileName);
        }
        if (!StringUtils.isEmpty(pcaid)) {
            if ("0".equals(pcatype)) {
                // 省
                queryWrapper.eq("pid", Integer.parseInt(pcaid));
            } else if ("1".equals(pcatype)) {
                // 市
                queryWrapper.eq("cid", Integer.parseInt(pcaid));
            } else {
                // 区
                queryWrapper.eq("aid", Integer.parseInt(pcaid));
            }
        }
        queryWrapper.eq("file_belongto", 0).and((wrapper) -> wrapper.eq("verify_state", 1).or().isNull("verify_state")).orderByAsc("file_type");
        baseMapper.selectPage(filePage, queryWrapper);
        return filePage;
    }

    @Override
    public Page<HzFileEntity> getApprovalFile(Map<String, Object> params) {
        String pcatype = (String) params.get("pcatype");
        String verifyState = (String) params.get("verifyState");
        String pcaid = (String) params.get("pcaid");
        String fileName = (String) params.get("fileName");
        long page = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        Page<HzFileEntity> filePage = new Page<>(page, limit);
        QueryWrapper<HzFileEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("file_belongto", 0).eq("file_type", 1);
        if (!StringUtils.isEmpty(pcaid)) {
            if ("0".equals(pcatype)) {
                // 省
                queryWrapper.eq("pid", Integer.parseInt(pcaid));
            } else if ("1".equals(pcatype)) {
                // 市
                queryWrapper.eq("cid", Integer.parseInt(pcaid));
            } else {
                // 区
                queryWrapper.eq("aid", Integer.parseInt(pcaid));
            }
        }
        if(!StringUtils.isEmpty(verifyState)) {
            queryWrapper.eq("verify_state", Integer.parseInt(verifyState));
        }
        if(!StringUtils.isEmpty(fileName)) {
            queryWrapper.like("file_name", fileName);
        }
        baseMapper.selectPage(filePage, queryWrapper);
        return filePage;
    }

}
